/*
 * Copyright (c) 2006-2021, RT-Thread Development Team
 *
 * SPDX-License-Identifier: Apache-2.0
 *
 * Change Logs:
 * Date           Author       Notes
 * 2022-03-18     desktop       the first version
 */

#include <all_code.h>

/*  导入log工具 */
#define DBG_TAG "foot.c"
#define DBG_LVL DBG_LOG
#include <rtdbg.h>

/*  脚部线程控制块 */
static rt_thread_t foot_scheduling_tid = RT_NULL;

/* 脚部调度信号量 */
struct rt_semaphore foot_sch_semaphore;

rt_uint8_t foot_scheduling_flag = 0;
//前面两个线程将自动接收数据，这里要创建一个线程来完成任务调度


void foot_motor_forward(void)
{
    rt_uint32_t pwm_temp = get_foot_pwm_pulse(temp_data[5]);
    //设置电机是正转
    pwm_foot_set(pwm_temp,0);
}

void foot_motor_reverse(void)
{
    rt_uint32_t pwm_temp = get_foot_pwm_pulse(temp_data[5]);
    //设置电机是正转
    pwm_foot_set(0,pwm_temp);
}

void foot_motor_stop(void)
{
    pwm_foot_set(0,0);
}


//电机要完成正转，停止，反转，停止。通过标志位去控制，这几个函数只调用一次
//任务调度函数通过信号量一直循环

static void foot_scheduling_entry(void *param)
{

    while(1)
    {
        //说明有数据变化，来查看一次数据调度
        rt_sem_take(&foot_sch_semaphore, RT_WAITING_FOREVER);

        //开关是打开的
        if(temp_data[6] == 1)
        {
            if(foot_scheduling_flag == 0)
            {
                //打开定时器，固定时间延迟，在中断将 标志位++，这边来 设置工作模式
                foot_timeout_s.sec = 1; /* 秒 */
                foot_timeout_s.usec = 0; /* 微 秒 */
                rt_device_write(foot_hw_time_dev, 0, &foot_timeout_s, sizeof(foot_timeout_s));
                LOG_I("                         启动脚部硬件定时器\t\n");
            }

            //正转
            if(foot_scheduling_flag == 1 && temp_data[6] == 1)
            {
                //更改定时器定时值
                foot_timeout_s.sec = 2; /* 秒 */
                rt_device_write(foot_hw_time_dev, 0, &foot_timeout_s, sizeof(foot_timeout_s));
                //这里开始调用PWM正转，转完之后在中断里给foot_scheduling_flag ++
                foot_motor_forward();
                LOG_I("                         脚部正转\t\n");
            }

            else if(foot_scheduling_flag == 2 && temp_data[6] == 1)
            {
                //更改定时器定时值
                foot_timeout_s.sec = 1; /* 秒 */
                rt_device_write(foot_hw_time_dev, 0, &foot_timeout_s, sizeof(foot_timeout_s));
                //这里开始调用PWM停止，转完之后在中断里给foot_scheduling_flag ++
                foot_motor_stop();
                LOG_E("                         脚部停止1\t\n");
            }

            else if(foot_scheduling_flag == 3 && temp_data[6] == 1)
            {
                //更改定时器定时值
                foot_timeout_s.sec = 2; /* 秒 */
                rt_device_write(foot_hw_time_dev, 0, &foot_timeout_s, sizeof(foot_timeout_s));
                //这里开始调用PWM反转，转完之后在中断里给foot_scheduling_flag ++
                foot_motor_reverse();
                LOG_I("                         脚部反转\t\n");
            }

            else if(foot_scheduling_flag == 4 && temp_data[6] == 1)
            {
                //更改定时器定时值
                foot_timeout_s.sec = 1; /* 秒 */
                rt_device_write(foot_hw_time_dev, 0, &foot_timeout_s, sizeof(foot_timeout_s));
                //这里开始调用PWM停止，转完之后在中断里给foot_scheduling_flag ++
                foot_motor_stop();
                LOG_E("                         脚部停止2\t\n");
            }

        }

        else if(temp_data[6] == 0)
        {
            //关闭定时器，清空PWM，标志位清空
            //LOG_I("关闭硬件定时器\t\n");
            rt_device_control(foot_hw_time_dev,HWTIMER_CTRL_STOP,&freq);
            foot_motor_stop();
            foot_scheduling_flag = 0;
            LOG_E("                         脚部关机\t\n");
        }
    }
}

void foot_app_creative(void)
{
    //初始化信号量
    rt_sem_init(&foot_sch_semaphore, "foot_sch_semaphore", 0, RT_IPC_FLAG_PRIO);

    //创建脚部调度线程并启动
    foot_scheduling_tid = rt_thread_create("foot_sch", foot_scheduling_entry, RT_NULL, 512, 14, 10);
    if(foot_scheduling_tid != RT_NULL)
    {
        rt_thread_startup(foot_scheduling_tid);
    }
}

